package com.vf.admin.client.service.impl;

import com.vf.admin.client.dao.*;
import com.vf.admin.client.service.AppUserDressupService;
import com.vf.admin.common.splitpage.SplitPageBean;
import com.vf.admin.miaodong.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import static com.vf.admin.utils.UseDate.getDay;

/**
 * 购买坐骑记录
 */
@Service
public class AppUserDressupServiceImpl implements AppUserDressupService {

    @Autowired
    AppUserDressupDao appUserDressupDao;

    @Autowired
    AppMingXiZuanShiDao appMingXiZuanShiDao;

    @Autowired
    AppDressuprecordDao appDressuprecordDao;

    @Autowired
    AppUserDao appUserDao;


    @Autowired
    AppMountsDao appMountsDao;

    @Autowired
    AppHeadwearDao appHeadwearDao;

    @Override
    public List<AppUserDressup> list(Map<String, Object> paraMap, SplitPageBean splitPage) {
        return appUserDressupDao.list(paraMap, splitPage);
    }

    @Override
    public List<AppUserDressup> list(Map<String, Object> paraMap) {
        return appUserDressupDao.list(paraMap);
    }

    @Override
    public void save(AppUserDressup haoYou) {
        appUserDressupDao.save(haoYou);
    }

    @Override
    public void updateZuoqiDate(Integer mountsid,Integer type,String goodpic,String goodname) {
        appUserDressupDao.updateZuoqiDate(mountsid,type,goodpic,goodname);
    }

    @Override
    public void del(Integer id) {
        appUserDressupDao.del(id);
    }

    @Override
    public AppUserDressup get(Integer id) {
        return appUserDressupDao.get(id);
    }
    @Override
    public AppUserDressup getUserid(Integer userid,Integer type) {
        return appUserDressupDao.getUserid(userid,type);
    }

    @Override
    public List<AppUserDressup> getPiliang( String arr,Integer type) {
        return appUserDressupDao.getPiliang(arr,type);
    }

    @Override
    @Transactional(rollbackFor = java.lang.Exception.class)
    public boolean shopZuoqi(AppUser user,AppMounts mounts) {
        try{
            if(user.getDiamonds().compareTo(mounts.getMoney()) < 0) return false;
            //更新用户钻石
            appUserDao.upJianZuanShi(user.getId(),mounts.getMoney());
            //获取自己已经购买的坐骑  如果有未过期的在这基础上加总天数
            AppUserDressup appUserDressup = appUserDressupDao.selectByUserid(user.getId(), mounts.getId(), 1);
            SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            if (org.springframework.util.StringUtils.isEmpty(appUserDressup)) {
                //为购买
                AppUserDressup dressup = new AppUserDressup();
                dressup.setBuyingtime(new Date());
                dressup.setDay(mounts.getDay());
                dressup.setMountsid(mounts.getId());
                dressup.setState(2);
                dressup.setType(1);
                dressup.setPic(user.getPic());
                dressup.setPhone(user.getLoginname());
                dressup.setNick(user.getNick());
                dressup.setUserid(user.getId());
                String overTime=getDay(dressup.getDay(),new Date());
                dressup.setOvertime(f.parse(overTime));
                dressup.setGoodname(mounts.getName());
                dressup.setGoodpic(mounts.getImgurl());
                appUserDressupDao.save(dressup);
            } else {
                Integer day = appUserDressup.getDay();

                Date overYuanTime = new Date();
                if(appUserDressup.getOvertime() != null) {
                    overYuanTime = appUserDressup.getOvertime();
                }
                String overTime = getDay(mounts.getDay(),overYuanTime);
                appUserDressup.setOvertime(f.parse(overTime));
                appUserDressup.setDay(day + mounts.getDay());
                appUserDressupDao.save(appUserDressup);
            }
            //添加明细
            AppMingXiZuanShi zuanShi = new AppMingXiZuanShi();
            zuanShi.setUserid(user.getId());
            zuanShi.setPic(user.getPic());
            zuanShi.setPhone(user.getLoginname());
            zuanShi.setNick(user.getNick());
            zuanShi.setLeixing(2);
            zuanShi.setNum(mounts.getMoney());
            zuanShi.setCreatetime(new Date());
            zuanShi.setBeizhu("购买坐骑");
            appMingXiZuanShiDao.saveOrUpdate(zuanShi);
            return true;
        }catch (Exception e){
            //导入失败  事务回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
    }


    @Override
    @Transactional(rollbackFor = java.lang.Exception.class)
    public boolean shopToushi(AppUser user,AppHeadwear headwear) {
        try{
            if(headwear.getMold().equals(1)){
                if(user.getDiamonds().compareTo(headwear.getMoney()) < 0) return false;
                //更新用户钻石
                appUserDao.upJianZuanShi(user.getId(),headwear.getMoney());
            }
            //获取自己已经购买的头饰  如果有未过期的在这基础上加总天数
            AppUserDressup appUserDressup = appUserDressupDao.selectByUserid(user.getId(), headwear.getId(), 2);
            //等级头饰只能购买一次
            if(headwear.getMold().equals(2) && Objects.nonNull(appUserDressup)) return false;
            SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            if (Objects.isNull(appUserDressup)) {
                //购买新增记录
                AppUserDressup dressup = new AppUserDressup();
                dressup.setBuyingtime(new Date());
                dressup.setDay(headwear.getDay());
                dressup.setMountsid(headwear.getId());
                dressup.setState(2);
                dressup.setType(2);
                dressup.setPic(user.getPic());
                dressup.setPhone(user.getLoginname());
                dressup.setNick(user.getNick());
                dressup.setUserid(user.getId());
                String overTime=getDay(headwear.getDay(),new Date());
                dressup.setOvertime(f.parse(overTime));
                dressup.setGoodname(headwear.getName());
                dressup.setGoodpic(headwear.getImgurl());
                appUserDressupDao.save(dressup);
            } else {
                Integer day = appUserDressup.getDay();

                Date overYuanTime = new Date();
                if(appUserDressup.getOvertime() != null) {
                    overYuanTime = appUserDressup.getOvertime();
                }
                String overTime=getDay(headwear.getDay(),overYuanTime);
                appUserDressup.setDay(day + headwear.getDay());
                appUserDressup.setOvertime(f.parse(overTime));
                appUserDressupDao.save(appUserDressup);
            }
            //添加明细
            AppMingXiZuanShi zuanShi = new AppMingXiZuanShi();
            zuanShi.setUserid(user.getId());
            zuanShi.setPic(user.getPic());
            zuanShi.setPhone(user.getLoginname());
            zuanShi.setNick(user.getNick());
            zuanShi.setLeixing(2);
            if(headwear.getMold().equals(1)) {
                zuanShi.setNum(headwear.getMoney());
            }else{
                zuanShi.setNum(BigDecimal.ZERO);
            }
            zuanShi.setCreatetime(new Date());
            zuanShi.setBeizhu("购买头饰");
            appMingXiZuanShiDao.saveOrUpdate(zuanShi);
            return true;
        }catch (Exception e){
            //导入失败  事务回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
    }

    @Override
    public List<AppUserDressup> selectMountsByUserid(Integer userid, Integer type) {
        return appUserDressupDao.selectMountsByUserid(userid,type);
    }
    @Override
    public List<AppUserDressup> selectNoGuoqi() {
        return appUserDressupDao.selectNoGuoqi();
    }
}
